package com.wlcorp.forcazamis.dao.system.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ibatis.dao.client.DaoException;
import com.wlcorp.forcazamis.dao.system.IStaffDAO;
import com.wlcorp.forcazamis.domain.system.Staff;
import com.wlcorp.forcazamis.sys.util.AppConstant;
import com.wlcorp.forcazamis.sys.util.Validator;

public class StaffDAOImpl extends HibernateDaoSupport implements IStaffDAO {
	
	public Staff getStaffByCodeAndPassword(String staffCode, String password) throws DaoException{
		try {
			String queryString = "from Staff as model where model.deleteFlag=false and model.staffCode = :staffCode and model.password = :password";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setString("staffCode", staffCode);
			queryObject.setString("password", password);
			return (Staff)queryObject.uniqueResult();
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List getStaffList(final Staff staff) throws DaoException{
		final StringBuffer queryString = new StringBuffer("select staff from Staff as staff where staff.deleteFlag=false and staff.flag = ").append(AppConstant.RECORD_UNDELETED)
			.append(" and staff.staffCode not in('admin','administrator')");
		if(null != staff){
			if(Validator.checkValueIsValid(staff.getStaffName())){
				queryString.append(" and staff.staffName like '%").append(staff.getStaffName()).append("%'");
			}
			if(null != staff.getDepartmentId()){
				queryString.append(" and staff.department.departmentId = ").append(staff.getDepartmentId());
			}
			if(null != staff.getRoleId()){
				queryString.append(" and staff.role.roleId = ").append(staff.getRoleId());
			}
			if(null != staff.getStaffCode()){
				queryString.append(" and staff.staffCode = '").append(staff.getStaffCode()).append("'");
			}
			
			if(null != staff.getDepotId()){
				queryString.append(" and staff.depot.depotId = ").append(staff.getDepotId());
			}
		}
		return getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				if(null != staff && null != staff.getMinRownum()){
					query.setFirstResult(staff.getMinRownum());
					query.setMaxResults(staff.getNumPerPage());
				}
				return query.list();
			}
			
		});
	}
	
	public Long getStaffCount(final Staff staff) throws DaoException{
		final StringBuffer queryString = new StringBuffer("select count(staff) from Staff as staff where staff.deleteFlag=false and staff.flag = ").append(AppConstant.RECORD_UNDELETED)
			.append(" and staff.staffCode not in('admin','administrator')");
		if(null != staff){
			if(Validator.checkValueIsValid(staff.getStaffName())){
				queryString.append(" and staff.staffName like '%").append(staff.getStaffName()).append("%'");
			}
			if(null != staff.getDepartmentId()){
				queryString.append(" and staff.department.departmentId = ").append(staff.getDepartmentId());
			}
			if(null != staff.getRoleId()){
				queryString.append(" and staff.role.roleId = ").append(staff.getRoleId());
			}
			if(null != staff.getStaffCode()){
				queryString.append(" and staff.staffCode = '").append(staff.getStaffCode()).append("'");
			}
			if(null != staff.getDepotId()){
				queryString.append(" and staff.depot.depotId = ").append(staff.getDepotId());
			}
		}
		return (Long)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				return query.uniqueResult();
			}
			
		});
	}
	public Staff getStaffById(Long staffId) throws DaoException{
		
		return (Staff)getHibernateTemplate().get(Staff.class,staffId);
	}

	public void deleteStaff(Staff staff) throws DaoException{
		staff.setDeleteFlag(true);
		getHibernateTemplate().update(staff);
	}

	public void saveStaff(Staff staff) throws DaoException{
		getHibernateTemplate().saveOrUpdate(staff);
	}

}
